/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.tb_discharge_cargo_address.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.tb_areas.service.TbAreasService;
import com.thinkgem.jeesite.modules.tb_discharge_cargo_address.entity.TbDischargeCargoAddress;
import com.thinkgem.jeesite.modules.tb_discharge_cargo_address.service.TbDischargeCargoAddressService;

/**
 * 卸货地址管理Controller
 * @author ZhangYangZe
 * @version 2017-05-13
 */
@Controller
@RequestMapping(value = "${adminPath}/tb_discharge_cargo_address/tbDischargeCargoAddress")
public class TbDischargeCargoAddressController extends BaseController {

	@Autowired
	private TbDischargeCargoAddressService tbDischargeCargoAddressService;
	@Autowired
	private TbAreasService tbAreasService;
	/**
	 *获取单条数据的方法
	 *@param TbDischargeCargoAddress tbDischargeCargoAddress
	 *@param RedirectAttributes redirectAttributes
	 *@return String
	 */
	@ModelAttribute
	public TbDischargeCargoAddress get(@RequestParam(required=false) String id) {
		TbDischargeCargoAddress entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tbDischargeCargoAddressService.get(id);
		}
		if (entity == null){
			entity = new TbDischargeCargoAddress();
		}
		return entity;
	}
	/**
	 *查询分页数据
	 *@param TbDischargeCargoAddress 
	 *@param HttpServletRequest
	 *@param Model model
	 *@param HttpServletResponse
	 *@return String
	 */
	@RequiresPermissions("tb_discharge_cargo_address:tbDischargeCargoAddress:view")
	@RequestMapping(value = {"list", ""})
	public String list(TbDischargeCargoAddress tbDischargeCargoAddress, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TbDischargeCargoAddress> page = tbDischargeCargoAddressService.findPage(new Page<TbDischargeCargoAddress>(request, response), tbDischargeCargoAddress); 
		model.addAttribute("page", page);
		return "modules/tb_discharge_cargo_address/tbDischargeCargoAddressList";
	}
	/**
	 *查询地区的方法
	 *@param TbDischargeCargoAddress tbDischargeCargoAddress
	 *@param Model model
	 *@return String
	 */
	@RequiresPermissions("tb_discharge_cargo_address:tbDischargeCargoAddress:view")
	@RequestMapping(value = "form")
	public String form(TbDischargeCargoAddress tbDischargeCargoAddress, Model model) {
		model.addAttribute("tbAreas", tbAreasService.findCityByParentId("0"));
		model.addAttribute("tbDischargeCargoAddress", tbDischargeCargoAddress);
		return "modules/tb_discharge_cargo_address/tbDischargeCargoAddressForm";
	}
	/**
	 *保存数据（插入或更新）的方法
	 *@param TbDischargeCargoAddress tbDischargeCargoAddress
	 *@param RedirectAttributes redirectAttributes
	 *@param Model model
	 *@return String
	 */
	@RequiresPermissions("tb_discharge_cargo_address:tbDischargeCargoAddress:edit")
	@RequestMapping(value = "save")
	public String save(TbDischargeCargoAddress tbDischargeCargoAddress, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tbDischargeCargoAddress)){
			return form(tbDischargeCargoAddress, model);
		}
		tbDischargeCargoAddressService.save(tbDischargeCargoAddress);
		addMessage(redirectAttributes, "保存卸货地址成功");
		return "redirect:"+Global.getAdminPath()+"/tb_discharge_cargo_address/tbDischargeCargoAddress/?repage";
	}
	/**
	 *卸货地区、公司、联系方式的删除方法
	 *@param TbDischargeCargoAddress tbDischargeCargoAddress
	 *@param RedirectAttributes redirectAttributes
	 *@return String
	 */
	@RequiresPermissions("tb_discharge_cargo_address:tbDischargeCargoAddress:edit")
	@RequestMapping(value = "delete")
	public String delete(TbDischargeCargoAddress tbDischargeCargoAddress, RedirectAttributes redirectAttributes) {
		tbDischargeCargoAddressService.delete(tbDischargeCargoAddress);
		addMessage(redirectAttributes, "删除卸货地址成功");
		return "redirect:"+Global.getAdminPath()+"/tb_discharge_cargo_address/tbDischargeCargoAddress/?repage";
	}
	
	/**
	 *卸货地区、公司、联系方式等详情展示 
	 *@param HttpServletRequest
	 *@param HttpServletResponse
	 *@param Model
	 *@return ArrayList<TbDischargeCargoAddress>
	 */
	@RequestMapping("addressDetails")
	@ResponseBody
	public ArrayList<TbDischargeCargoAddress> addressDetails( HttpServletRequest request, HttpServletResponse response, Model model){
		String id = request.getParameter("id");
		ArrayList<TbDischargeCargoAddress> tbDischargeCargoAddress1 = tbDischargeCargoAddressService.addressDetails(id);
		return tbDischargeCargoAddress1;
	}
}